package co.yixiang.modules.monitoring.serviceimpl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.adopt.entity.domain.YxAdoptCategory;
import co.yixiang.modules.adopt.entity.vo.YxAdoptCategoryVo;
import co.yixiang.modules.monitoring.entity.domain.YxMonitoringCategory;
import co.yixiang.modules.monitoring.entity.dto.YxMonitoringCategoryDto;
import co.yixiang.modules.monitoring.entity.query.YxMonitoringCategoryQueryCriteria;
import co.yixiang.modules.monitoring.entity.vo.YxMonitoringCategoryVo;
import co.yixiang.modules.monitoring.mapper.MonitoringCategoryMapper;
import co.yixiang.modules.monitoring.service.YxMonitoringCategoryService;
import co.yixiang.modules.mp.entity.domain.YxArticle;
import co.yixiang.modules.mp.entity.domain.YxArticleCategory;
import co.yixiang.modules.mp.entity.dto.YxArticleCategoryDto;
import co.yixiang.modules.mp.entity.vo.YxArticleCategoryVo;
import co.yixiang.modules.mp.entity.vo.YxArticleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true,rollbackFor = Exception.class)
public class YxMonitoringCategoryServiceImpl extends BaseServiceImpl<MonitoringCategoryMapper, YxMonitoringCategory> implements YxMonitoringCategoryService {
    @Autowired
    private IGenerator generator;

    @Override
    public List<YxMonitoringCategoryVo> queryAllByVo() {
        return generator.convert(this.list(),YxMonitoringCategoryVo.class);

    }

    @Override
    public Map<String, Object> queryAll(YxMonitoringCategoryQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxMonitoringCategory> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxMonitoringCategoryDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<YxMonitoringCategory> queryAll(YxMonitoringCategoryQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxMonitoringCategory.class, criteria));
    }

    @Override
    public Map<String, Object> buildTree(List<YxMonitoringCategoryDto> monitoringCategoryDtoList) {
        List<YxMonitoringCategoryDto> trees = new ArrayList<>();
        Set<Integer> ids = new HashSet<>();
        for (YxMonitoringCategoryDto categoryDto : monitoringCategoryDtoList) {
            if (categoryDto.getParentId() == 0) {
                trees.add(categoryDto);
            }
            for (YxMonitoringCategoryDto it : monitoringCategoryDtoList) {
                if (it.getParentId().equals(categoryDto.getId())) {
                    if (categoryDto.getChildren() == null) {
                        categoryDto.setChildren(new ArrayList<>());
                    }
                    categoryDto.getChildren().add(it);
                    ids.add(it.getId());
                }
            }
        }
        Map<String, Object> map = new HashMap<>(2);
        if (trees.size() == 0) {
            trees = monitoringCategoryDtoList.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
        }
        map.put("content", trees);
        map.put("totalElements", monitoringCategoryDtoList.size());
        return map;
    }

    @Override
    public List<YxMonitoringCategoryDto> buildYxMonitoringCategorys(List<YxMonitoringCategoryDto> monitoringCategoryDtoList) {

        return null;
    }

    @Override
    public Object getYxMonitoringCategoryTree(List<YxMonitoringCategory> monitoringCategoryList) {
        List<Map<String, Object>> list = new LinkedList<>();
        monitoringCategoryList.forEach(category -> {
                    if (category != null) {
                        List<YxMonitoringCategory> categoryList = findByPid(category.getId());
                        Map<String, Object> map = new HashMap<>(16);
                        map.put("id", category.getId());
                        map.put("label", category.getName());
                        if (categoryList != null && categoryList.size() != 0) {
                            map.put("children", getYxMonitoringCategoryTree(categoryList));
                        }
                        list.add(map);
                    }
                }
        );
        return list;
    }

    @Override
    public List<YxMonitoringCategory> findByPid(Integer pid) {
        return list(new LambdaQueryWrapper<YxMonitoringCategory>().eq(YxMonitoringCategory::getParentId, pid));
    }
}
